package com.egobuy.egobuysearch.controller;

import com.egobuy.search.pojo.SearchItem;
import com.egobuy.search.pojo.SearchResponseEntity;
import com.igeekhome.egobuy.feign.SearchClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author yadonghe
 * @date 2020-03-09 09:21
 */
@Controller
public class PageController {

    @Autowired
    private SearchClients searchClients;
    /**
     * 根据关键词（portal搜索框输入的)进行搜索
     * @param keyword
     * @return
     */
    @RequestMapping("/Search")
    public String search(String keyword, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, Model model) {
        //根据关键词进行查询（数据库？索引库）
        //resttemplate -> http://localhost:8090/index/select?keyword=%E5%B0%8F%E7%B1%B3
        SearchResponseEntity searchResponseEntity = searchClients.search(keyword, page, limit);
        //query 查询关键字
        //totalPages 总页数
        //page 当前页
        //recourdCount 总条数
        //itemList 数据集合
        model.addAttribute("query", keyword);
        model.addAttribute("recourdCount", searchResponseEntity.getTotal());
        model.addAttribute("page", page);
        model.addAttribute("itemList", searchResponseEntity.getItems());
        model.addAttribute("totalPages", (int)(Math.ceil(searchResponseEntity.getTotal() / (limit * 1.0))));
        return "search";
    }
}
